package cz.jm.web.manhunter.client.common;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;

import cz.jm.web.manhunter.client.ResorcesInfo;

/**
 * Horizontalny layout s nastavenymi zakladnymi standardnymi nastaveniami pre aplikaciu, 
 * tak aby nebolo nutne ich opakovane nastavovat.
 * Dalej umoznuje zobrazenie tlacitka v pravom hornom rohu + pridanie prislusnej obsluhy.
 * 
 * */
public class StandardDecoratedPanel implements Panel {
	
	private static final String LAYOUT_BORDER_STYLE = "solid 1px blue";
	private static final String LAYOUT_BACKGROUND_COLOR = "#F0F0F0 ";
	private static final int LAYOUT_MARGIN_SIZE = 5;
	private static final int LAYOUT_CLOSE_ICON_SIZE = 16;
	/** Hlavny layout v ktorom vsetko lezi. */
	private final HLayout wrapperLayout = new HLayout();
	/** Pracovny layout s ktorym by mal user pracovat priamo, 
	 * tj. do ktoreho by mal uzivatel pridavat svoje polozky. */
	private final HLayout workLayout = new HLayout();
	/** Talcitko ktore je mozne zobrazit v zahlavi. */
	private final ImgButton headerButton = new ImgButton();
	
	
	public StandardDecoratedPanel() {
		wrapperLayout.setWidth100();
		wrapperLayout.setAutoHeight();
		wrapperLayout.setMargin(LAYOUT_MARGIN_SIZE);
		wrapperLayout.setBorder(LAYOUT_BORDER_STYLE);
		wrapperLayout.setBackgroundColor(LAYOUT_BACKGROUND_COLOR);
		
		workLayout.setWidth100();
		workLayout.setHeight100();
		workLayout.setAlign(Alignment.CENTER);
		

		headerButton.setWidth(LAYOUT_CLOSE_ICON_SIZE);
		headerButton.setHeight(LAYOUT_CLOSE_ICON_SIZE);
		headerButton.setShowRollOver(false);
		headerButton.setShowDown(false);
		headerButton.setSrc(ResorcesInfo.getIconBlueCross());			
				
		wrapperLayout.addMember(workLayout);
	}
	
	/** Nastavenie cesty k obrazku-ikone, ktora sa pouzije v zahlavi ako button. */
	public void setHeaderButtonIconPath(String path) {
		headerButton.setSrc(path);
	}

	/** Vrati cestu k obrazku-ikone, ktora sa pouzije v zahlavi ako button. 
	 * 	Defaultne je nastaveny krizik - tj. ocakava sa obsluha zatvorenia. 
	 * */
	public String getHeaderButtonIconPath() {
		return headerButton.getSrc();
	}
	
	public void setHeaderButtonVisible(boolean show) {
		if (show) {
			wrapperLayout.addMember(headerButton);
		}
		else {
			wrapperLayout.removeMember(headerButton);
		}
	}
	
	public void setHeaderCloseButton(boolean show) {
		setHeaderButtonIconPath(ResorcesInfo.getIconBlueCross());
		setHeaderButtonVisible(true);
		addHeaderButtonClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				wrapperLayout.hide();
			}
		});
	}
	
	/**
	 * Pridanie CloseHandler-u pre obsluhu kliknutia na button v zahlavi layout. 
	 * 
	 * */
	public void addHeaderButtonClickHandler(com.smartgwt.client.widgets.events.ClickHandler handler) {		
		headerButton.addClickHandler(handler);		
	}

	/** Vracia horizontalny layout s ktorym je mozne plnohodnotne pracovat. 
	 * 	Predstavuje obsah okrem klikacieho buttonu v zahlavi. */
	public HLayout getWorkLayout() {
    	return workLayout;
    }
	
	/** Layout ako celeok tj. obsah + button v zahlavi + nastavenia. */
	public HLayout asHLayout() { return wrapperLayout; }

	@Override
    public Canvas asCanvas() {
	    return wrapperLayout;
    } 
		
}
